package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
/*    @PostMapping("/upload")
    public Result upload(String name , Integer age, MultipartFile file) throws Exception {
        log.info("{},{},{}", name, age, file);
        String originalFilename = file.getOriginalFilename();//获取原始文件名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));//将原始文件名以最后的.进行切割,获取文件后缀
        String newFileName = UUID.randomUUID().toString() + substring;//生成新的文件名(不能重复)-----UUID
        file.transferTo(new File("D:\\Desktop\\upload\\"+ newFileName));
        return Result.success();
    }*/
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("{}", image);
        String originalFilename = image.getOriginalFilename();//获取原始文件名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));//将原始文件名以最后的.进行切割,获取文件后缀
        String newFileName = UUID.randomUUID().toString() + substring;//生成新的文件名(不能重复)-----UUID
        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }

    }


